package com.eig.courseweb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eig.common.entity.course.Chapter;
import com.eig.common.entity.course.Video;
import com.eig.common.vo.Result;
import com.eig.courseweb.dao.ChapterDao;
import com.eig.courseweb.dao.VideoDao;
import com.eig.courseweb.service.ChapterService;
import com.eig.courseweb.utils.IdWorker;
import com.eig.courseweb.vo.CourseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：StudentLsj（李世界）
 * @date ：Created in 2023/01/11 下午 08:11
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ChapterServiceImpl implements ChapterService {
    @Resource
    private ChapterDao chapterDao;
    @Resource
    private VideoDao videoDao;

    @Override
    public Result findChapterById(String id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("course_id", id);
        List<Chapter> chapterList = chapterDao.selectList(wrapper);

        List<Video> videoList = videoDao.selectList(wrapper);

        for (Chapter chapter : chapterList) {
            List<Video> videos = new ArrayList<>();

            for (Video video : videoList) {
                if (video.getChapterId().equals(chapter.getId())) {
                    videos.add(video);
                }
            }
            chapter.setVideo(videos);
        }
        for (Chapter chapter : chapterList) {
            if (ObjectUtil.isNotEmpty(chapter.getVideo())){
                    chapter.setPeriodNum(chapter.getVideo().size());
            }
        }
        return new Result(2000, "查询成功", chapterList);
    }

    @Resource
    IdWorker idWorker;

    @Override
    public Result insertChapterWithCourseId(CourseVo courseVo) {
        if (ObjectUtil.isNotEmpty(courseVo.getChapterDesc())) {
            chapterDao.updateChapter(courseVo);
        } else {
            courseVo.setOneSubjectId(idWorker.nextId());
            chapterDao.insertChapterWithCourseId(courseVo);

        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("course_id", courseVo.getCourseId());
        List<Chapter> chapterList = chapterDao.selectList(wrapper);

        List<Video> videoList = videoDao.selectList(wrapper);

        for (Chapter chapter : chapterList) {
            List<Video> videos = new ArrayList<>();

            for (Video video : videoList) {
                if (video.getChapterId().equals(chapter.getId())) {
                    videos.add(video);
                }
            }
            chapter.setVideo(videos);
        }
        return new Result(2000, "操作成功", chapterList);
    }

    @Override
    public Result deleteChapterWithCourseId(Chapter courseVo) {
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",courseVo.getId());
        int delete = chapterDao.delete(queryWrapper);
        return new Result(2000, "操作成功", delete);
    }
}
